package stud.cj.day15;
    import java.sql.*;

public class StudentJdbcTest {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        //1. 加载驱动 - 省略不写
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2. 获取连接 - 获取和db的连接
            String url = "jdbc:mysql://localhost:3306/kd?useSSL=false";
            String user = "root";
            String password = "root";

            conn = DriverManager.getConnection(url,user,password);

            //发送sql语句到db-server端
            st = conn.createStatement();
            String sql = "select * from student where id=1";
            rs = st.executeQuery(sql);

            if(rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                int age = rs.getInt(3);
                // System.out.println(id+":"+name+":"+age);

                //Student s = new Student(id,name,age);

                Student s = new Student();
                s.setId(id);
                s.setName(name);
                s.setAge(age);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(null!=rs){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(null!=st){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(null!=conn){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
